package com.skycode7.dao;

import com.skycode7.bean.AdminBean;
import com.skycode7.bean.NoticeBean;
import com.skycode7.util.Dbconnection;
import com.skycode7.util.websiteSample;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    websiteSample DBConn=new websiteSample();
    public boolean CheckLogin(String s1,String s2) throws SQLException{
        Connection conn=DBConn.getConn();
        Statement statement = conn.createStatement();
        ResultSet rs = null;
        boolean OK = true;
        String sql ="select * from admin where AdminName = '" +s1 +
                "'and AdminPwd = '" + s2 + "'";
        rs = statement.executeQuery(sql);
        if (!rs.next()){
            OK = false;
        }else {
            OK = true;
        }
        return OK;
    }
    Dbconnection db =new Dbconnection();
    public int add(AdminBean news) {
        Connection conn = db.getConn();
        String sql = "insert into news veluse(null,?,?,?,?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,news.getAdminName());
            ps.setString(2,news.getAdminPwd());
            ps.setInt(3,news.getAdminID());
            ps.setInt(4,news.getAdminType());
        }catch (SQLException e){
            System.out.println("插入news表异常");
        }
        return 0;
    }
    public int delete(int AdminID) {
        Connection conn = db.getConn();
        String sql = "delete from news where noticeId = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, AdminID);
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("删除news表异常");
        }
        return 0;//如果添加异常 返回0
    }
    public int update(AdminBean news) {
        Connection conn = db.getConn();//conn代表mysql连接
        String sql = "update news set NoticeTitle=?,NotitleContent=?,NoticeTime=?,AdminName=? where NoticeID=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //将新闻数据对象中的数据依次绑定到sql命令中
            ps.setString(1, news.getAdminName());
            ps.setString(2, news.getAdminPwd());
            ps.setInt(3, news.getAdminID());
            ps.setString(4, news.getAdminName());
            return ps.executeUpdate();//执行sql命令 完成数据插入
        } catch (SQLException e) {
            System.out.println("更新news表异常");
        }
        return 0;//如果添加异常 返回0
    }
    public List<AdminBean> getList(int page){
        Connection conn = db.getConn();
        String sql ="select * from news limit ?,?";
        List<AdminBean> list = new ArrayList<>();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,(page-1)*10);
            ps.setInt(2,10);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                AdminBean news = new AdminBean();
                news.setAdminID(rs.getInt("NoticeId"));
                news.setAdminPwd(rs.getString("AdminPwd"));
                news.setAdminType(rs.getInt("AdminType"));
                news.setAdminName(rs.getString("AdminName"));
                news.setLastLoginTime(rs.getString("LastLoginTime"));
                list.add(news);
            }
        } catch (SQLException e){
            System.out.println("查询news表异常");
        }
        return list;
    }
}
